/*
 * camera.h
 *
 *  Created on: 2024骞�6鏈�6鏃�
 *      Author: Merlin_yang
 */

#ifndef CAMERA_H_
#define CAMERA_H_

#include "zf_common_typedef.h"
#include "zf_device_mt9v03x.h"
#include "math_plus.h"
#include "leg_control.h"

extern float turn_angle;
extern uint8 stop_flag;
extern int const_C_bright, const_C_dark, brightness_split_point, ABSOLUTE_BLACK_THRESHOLD;
#define IMAGE_WIDTH  188
#define IMAGE_HEIGHT 120
static int g_last_frame_center_x = IMAGE_WIDTH / 2;
#define SMALL_IMAGE_WIDTH  (IMAGE_WIDTH / 2)
#define SMALL_IMAGE_HEIGHT (IMAGE_HEIGHT / 2)

#define PIXEL_TRACK      255
#define PIXEL_BACKGROUND 0

#define START_SCAN_OFFSET_FROM_BOTTOM 12

#define SLIDING_WINDOW_SIZE 40 

#define DEGREES_TO_RADIANS(angle) ((angle) * M_PI / 180.0f)

extern IFX_ALIGN(4) uint8  original_image[MT9V03X_H][MT9V03X_W];

// =================================================================================================
// 绫诲瀷瀹氫箟 (Type Definitions)
// =================================================================================================

// 瀹氫箟鐐圭粨鏋勪綋
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    float x;
    float y;
} Point2Df;

typedef enum {
    EDGE_LEFT,
    EDGE_RIGHT
} EdgeType;

extern volatile int time;
extern float h;
extern Point left_edge[IMAGE_HEIGHT * 2];
extern Point right_edge[IMAGE_HEIGHT * 2];
extern Point center_points[IMAGE_HEIGHT * 2];
extern Point left_world_pts[IMAGE_HEIGHT * 2];
extern Point right_world_pts[IMAGE_HEIGHT * 2];
extern Point left_resample_pts[IMAGE_HEIGHT * 2];
extern Point right_resample_pts[IMAGE_HEIGHT * 2];
extern Point center_resample_pts[IMAGE_HEIGHT * 2];
extern Point world_zebra_point[2], screen_zebra_point[2];
extern Point left_zebra, right_zebra;
extern int left_count, right_count, center_count, left_count_w, right_count_w, left_count_resample, right_count_resample, center_count_resample;
extern float turning_radius_m;
extern int real_zebra_distance;
extern IFX_ALIGN(4) uint8  original_image[MT9V03X_H][MT9V03X_W];
extern IFX_ALIGN(4) uint8  binary_image[MT9V03X_H][MT9V03X_W];

void adaptive_thresholding_fast(const unsigned char src_image[IMAGE_HEIGHT][IMAGE_WIDTH], unsigned char dst_image[IMAGE_HEIGHT][IMAGE_WIDTH]);
int test();
void detect_edges_eight_neighbor(
    const unsigned char binary_img[IMAGE_HEIGHT][IMAGE_WIDTH],
    Point left_edge_points[], Point right_edge_points[],
    int* left_count, int* right_count
);

typedef struct {
    Point prev_left_edge[IMAGE_HEIGHT * 2];
    Point prev_right_edge[IMAGE_HEIGHT * 2];
    int prev_left_count;
    int prev_right_count;
    int first_frame;
} DisplayState;

void display_edge_points_on_ips200_xy_multi(
    DisplayState* state,
    const Point left_edge[],
    const Point right_edge[],
    int left_count,
    int right_count,
    uint16_t left_color,
    uint16_t right_color
);

void process_path_int(
    const Point* path_in,
    uint8_t point_count_in,
    Point* path_out,
    uint8_t* point_count_out,
    const float* gaussian_kernel, // <--- 改动：传入高斯核
    int kernel_size,              // <--- 改动：传入核大小
    float step_distance
);
void detect_edges_eight_neighbor_cross(
    const unsigned char binary_img[IMAGE_HEIGHT][IMAGE_WIDTH],
    Point left_edge_points[], Point right_edge_points[],
    int* left_count, int* right_count
);

void camera_mt9v03x_init(void);
void save_image(void);
void generate_gaussian_kernel(float* kernel, int kernel_size, float sigma);
#endif /* CODE_CAMERA_H_ */
